import requests
from bs4 import BeautifulSoup

url='https://www.baidu.com'

headers={
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
}
r=requests.get(url,headers=headers)
soup= BeautifulSoup(r.content.decode(),'lxml')
# 查找标签
r1=soup.find('div',{'class':'title-text c-font-medium c-color-t'})
print(r1)
# 查找标签内的文本内容
r2=soup.find('div',{'class':'title-text c-font-medium c-color-t'}).string
print(r2)
# 查找某标签的属性值
# r3=soup.find_all('span',{'class','title-content-title'})
r3=soup.find_all('li',{'class','hotsearch-item'})
list_data=[]
for e in r3:
    # e是每个li
    # 获取文本内容，文本是第二个span标签
    name=e.select('span')[1].string
    # print(name)
    #
    #获取url
    url=e.find('a')['href']
    # print(url)
    data={'标题':name,'详情链接':url}
    print()
    print(data)
    list_data.append(data)
print(list_data)
with open('data.txt','w',encoding='utf-8') as f:
    f.write(list_data.__str__())
